/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spakehelpdesk.bean;

import com.spakehelpdesk.entity.Account;
import com.spakehelpdesk.java.EncryptionMd5;
import com.spakehelpdesk.java.SessionManage;
import com.spakehelpdesk.session.LoginSession;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author leehoanh
 */
@SessionScoped
public class LoginBean {

    @EJB
    private LoginSession loginSession;
    public String accountId = "";
    public String password = "";
    private SessionManage session;
    private Account login = new Account();

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Creates a new instance of LoginBean
     */
    public LoginBean() {
    }
    
    public String doLogin() {
        if (accountId.length() == 0 || password.length() == 0) {
            return "/faces/errorLogin";
        } else {
            login = loginSession.Login(accountId, EncryptionMd5.Encryption(password));
            if (login != null) {
                String role = login.getRoleID().getRoleID().toString();
                session.setValue("roleID", role);
                session.setValue("accountId", login.getAccountID());
                if (role.equals("1")) {
                    return "/Admin/ProfileAdmin";
                } else if (role.equals("2")) {
                    return "/Employee/Home";
                } else {
                    return "/Technicain/Home";
                }
            } else {

                return "/faces/login";
            }
        }

    }
}
